package org.scau.studentevaluation.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/8/26 15:41
 */
public class BeanConvertUtils {

    public static <VO, BO> BO VO2BO (VO vo, Class<BO> boClass) {
        if (vo == null || boClass == null) {
            return null;
        }
        BO bo = null;
        try {
            bo = boClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("实例化:" + boClass.getTypeName() + "异常");
        }
        BeanUtils.copyProperties(vo, bo);
        return bo;
    }

    public static <VO, BO> VO BO2VO (BO bo, Class<VO> voClass) {
        if (bo == null || voClass == null) {
            return null;
        }
        VO vo = null;
        try {
            vo = voClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("实例化:" + voClass.getTypeName() + "异常");
        }
        BeanUtils.copyProperties(bo, vo);
        return vo;
    }

    public static <VO, BO> List<BO> VOs2BOs (List<VO> vos, Class<BO> boClass) {
        if (vos == null || boClass == null) {
            return null;
        }
        List<BO> bos = new ArrayList<>();
        for (VO vo : vos) {
            BO bo = VO2BO(vo, boClass);
            bos.add(bo);
        }
        return bos;
    }

    public static <VO, BO> List<VO> BOs2VOs (List<BO> bos, Class<VO> voClass) {
        if (bos == null || voClass == null) {
            return null;
        }
        List<VO> vos = new ArrayList<>();
        for (BO bo : bos) {
            VO vo = BO2VO(bo, voClass);
            vos.add(vo);
        }
        return vos;
    }

    public static <BO, DO> DO BO2DO (BO bo, Class<DO> doClass) {
        if (bo == null || doClass == null) {
            return null;
        }
        DO ado = null;
        try {
            ado = doClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("实例化:" + doClass.getTypeName() + "异常");
        }
        BeanUtils.copyProperties(bo, ado);
        return ado;
    }

    public static <BO, DO> BO DO2BO (DO ado, Class<BO> boClass) {
        if (ado == null || boClass == null) {
            return null;
        }
        BO bo = null;
        try {
            bo = boClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("实例化:" + boClass.getTypeName() + "异常");
        }
        BeanUtils.copyProperties(ado, bo);
        return bo;
    }

    public static <BO, DO> List<DO> BOs2DOs (List<BO> bos, Class<DO> doClass) {
        if (bos == null || doClass == null) {
            return null;
        }
        List<DO> dos = new ArrayList<>();
        for (BO bo : bos) {
            DO ado = BO2DO(bo, doClass);
            dos.add(ado);
        }
        return dos;
    }

    public static <BO, DO> List<BO> DOs2BOs (List<DO> dos, Class<BO> boClass) {
        if (dos == null || boClass == null) {
            return null;
        }
        List<BO> bos = new ArrayList<>();
        for (DO ado : dos) {
            BO bo = DO2BO(ado, boClass);
            bos.add(bo);
        }
        return bos;
    }



}
